class FishingTechnique {
  final int id;
  final String title;
  final String subtitle;
  final String content;
  final String imageUrl;
  final String category;
  final String difficulty; // 初级、中级、高级
  final int readCount;
  final int likeCount;
  final int favoriteCount;
  final int commentCount;
  final List<String> tags;
  final String author;
  final String createTime;
  final int estimatedReadTime; // 预计阅读时间（分钟）
  final bool isLiked;
  final bool isFavorited;
  final String? favoriteTime;
  final String status; // 状态：0-禁用，1-启用
  final int sortOrder; // 排序字段
  final String? updateTime;

  FishingTechnique({
    required this.id,
    required this.title,
    required this.subtitle,
    required this.content,
    required this.imageUrl,
    required this.category,
    required this.difficulty,
    required this.readCount,
    required this.likeCount,
    required this.favoriteCount,
    required this.commentCount,
    required this.tags,
    required this.author,
    required this.createTime,
    required this.estimatedReadTime,
    required this.isLiked,
    required this.isFavorited,
    this.favoriteTime,
    this.status = '1',
    this.sortOrder = 0,
    this.updateTime,
  });

  factory FishingTechnique.fromJson(Map<String, dynamic> json) {
    // 处理tags字段，可能是字符串或列表
    List<String> tagsList = [];
    if (json['tags'] != null) {
      if (json['tags'] is String) {
        tagsList = (json['tags'] as String).split(',').where((tag) => tag.trim().isNotEmpty).toList();
      } else if (json['tags'] is List) {
        tagsList = List<String>.from(json['tags']);
      }
    }

    return FishingTechnique(
      id: json['id'] ?? 0,
      title: json['title'] ?? '',
      subtitle: json['subtitle'] ?? '',
      content: json['content'] ?? '',
      imageUrl: json['imageUrl'] ?? '',
      category: json['category'] ?? '',
      difficulty: json['difficulty'] ?? '',
      readCount: json['readCount'] ?? 0,
      likeCount: json['likeCount'] ?? 0,
      favoriteCount: json['favoriteCount'] ?? 0,
      commentCount: json['commentCount'] ?? 0,
      tags: tagsList,
      author: json['author'] ?? '钓技专家',
      createTime: json['createTime'] ?? '',
      estimatedReadTime: json['estimatedReadTime'] ?? 5,
      isLiked: json['isLiked'] ?? false,
      isFavorited: json['isFavorited'] ?? false,
      favoriteTime: json['favoriteTime'],
      status: json['status'] ?? '1',
      sortOrder: json['sortOrder'] ?? 0,
      updateTime: json['updateTime'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'subtitle': subtitle,
      'content': content, // Dio会自动处理JSON转义
      'imageUrl': imageUrl,
      'category': category,
      'difficulty': difficulty,
      'readCount': readCount,
      'likeCount': likeCount,
      'favoriteCount': favoriteCount,
      'commentCount': commentCount,
      'tags': tags.join(','), // 后端存储为逗号分隔的字符串
      'author': author,
      'createTime': createTime,
      'estimatedReadTime': estimatedReadTime,
      'isLiked': isLiked,
      'isFavorited': isFavorited,
      'favoriteTime': favoriteTime,
      'status': status,
      'sortOrder': sortOrder,
      'updateTime': updateTime,
    };
  }
}